package com.ifd.iview.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;

public class IviewUtils {
	public static String getContextPath(String resource){
		Execution exec = Executions.getCurrent();
		HttpServletRequest request = (HttpServletRequest)exec.getNativeRequest();
		String path = request.getContextPath().concat(resource);
		return path;
	}
	public static String getResourceURI(String resource){
		Execution exec = Executions.getCurrent();
		HttpServletResponse response = (HttpServletResponse)exec.getNativeResponse();
		HttpServletRequest request = (HttpServletRequest)exec.getNativeRequest();
		if(resource.startsWith(request.getContextPath()))
			return resource;
		else
			return response.encodeRedirectURL(getContextPath(resource));
	}
	public static void sendRedirect(String uri) throws IOException{
		Execution exec = Executions.getCurrent();
		HttpServletRequest request = (HttpServletRequest)exec.getNativeRequest();
		HttpServletResponse response = (HttpServletResponse)exec.getNativeResponse();
		String lnk = getResourceURI(uri);
		response.sendRedirect(lnk);
		exec.setVoided(true);
	}
}
